package com.liss.juc20211122.locks;

import java.util.concurrent.TimeUnit;

public class DeadLockDemo {

    static Object lockA = new Object();
    static Object lockB = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (lockA) {
                System.out.println(Thread.currentThread().getName() + "\t" + " 自己持有A锁，期待获得B锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lockB) {
                    System.out.println(Thread.currentThread().getName() + "\t" + " 获得B锁");
                }
            }
        }, "T1");
        t1.start();
        t1.interrupt();
        System.out.println(t1.isInterrupted());

        Thread t2 = new Thread(() -> {
            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() + "\t" + " 自己持有B锁，期待获得A锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (lockA) {
                    System.out.println(Thread.currentThread().getName() + "\t" + " 获得A锁");
                }
            }
        }, "T2");
        t2.start();
        t2.interrupt();
    }



}
